import emitter from "../emitter";
import gameMgr from "../gameMrg";

const { ccclass, property } = cc._decorator;
export var enemyPlane_slo: enemyPlane;
@ccclass
export class enemyPlane extends cc.Component {
    @property(cc.Boolean)
    public isMove = false;//是否可以移动
    @property(cc.Boolean)
    public isTrack = false;//是否跟踪玩家
    @property(cc.Boolean)
    public isAttack = false;//是否可以发起攻击
    @property(cc.Integer)
    public moveDirection = 0;//0 任意移动 1 上下 2 左右 3 跳跃 4 不移动
    @property(cc.Integer)
    public attackTime = 0;//多久攻击一次
    @property(cc.Integer)
    public bulletNum = 0;//每次发射几颗子弹
    @property(cc.Integer)
    public moveTime = 0;//多久移动一次
    @property(cc.Integer)
    public moveDistance = 0;//移动距离
    @property(cc.Integer)
    public moveSpeed = 0;//移动速率
    @property(cc.Node)
    nodeBullet: cc.Node = null;
    @property(cc.Prefab)
    pfBullet: cc.Prefab = null;
    isBoos = false;
    bulletPool = new cc.NodePool();

    __posData: any;
    onLoad() {
        this.initEventListener();
        this.initData();
        enemyPlane_slo = this;
    }

    initData() {
        this.isMove = true;
        this.isAttack = false;
        this.moveDirection = 0;
        this.moveTime = 0;
        this.moveDistance = 0;
        this.attackTime = 0;
        this.bulletNum = 0;
        if (this.isAttack) this.createBullet();
    }

    //创建对象池
    createBullet() {
        for (let i = 0; i < 200; i++) {
            let bullet = cc.instantiate(this.pfBullet);
            this.putBullet(bullet);
        }
    }

    //从对象池中获取子弹
    getBullet(number, minAngle, bulletAngle, nodeBullet) {
        if (this.isGameEnd) return
        let bullet;
        let nodeParent = nodeBullet.parent.parent;
        let worldPosi = nodeBullet.parent.convertToWorldSpaceAR(nodeBullet.position);
        let nodePosi = nodeParent.parent.convertToNodeSpaceAR(worldPosi);
        for (let i = 0; i < number; i++) {
            if (this.bulletPool.size() > 0) {
                bullet = this.bulletPool.get();
            }
            else {
                let node = cc.instantiate(this.pfBullet);
                this.putBullet(node);
                bullet = this.bulletPool.get();
            }
            let bulletItem = bullet.getComponent("enemyBullet");
            if (bulletItem) bulletItem.setIsTrack(this.isTrack);
            bullet.parent = nodeParent;
            bullet.setPosition(nodePosi);
            if (i == 0) bullet.angle = minAngle;
            else bullet.angle = minAngle + bulletAngle * i;
        }
    }

    //回收子弹
    putBullet(node: cc.Node) {
        node.removeFromParent();
        this.bulletPool.put(node);
    }

    initEventListener() {
        // emitter.on("putEnemyBullet", this.putBullet.bind(this));;
        emitter.on("gameEnd", this.gameEnd.bind(this));
    }

    onDisable() {
        emitter.off("gameEnd", this.gameEnd.bind(this));
    }

    isGameEnd = false;
    gameEnd() {
        this.isGameEnd = true;
    }

    setEndPosi(data) {
        // console.log("enemyPlane setEndPosi-------", data);
    }

    //设置节点移动
    setNodeMove(posi, time) {
        let disPos = posi.normalizeSelf();
        disPos.divSelf(time);
        this.__posData = disPos;
    }

    onPlaneDie(node: cc.Node) {
        //根据不同飞机类型来停止它的动作
        if (!node) return
        let name = node.name.split("_");
        switch (Number(name[1])) {
            case 0:
            case 3:
            case 1:
            case 5:
            case 8:
                // case 9:
                node.getComponent("enemyPlane_0").setNodeMove(cc.v2(0, 0), 0.01);
                break
            case 4:
                emitter.emit("createRedJi", node.position);
                break
            case 2:
            case 6:
            case 7:
            case 9:
            case 10:
            case 13:
                node.stopAllActions();
                break
        }
        //加载爆炸效果
        let callback = function (data) {
            let pfBoom = cc.instantiate(data);
            emitter.emit("setEnemyPlaneNumber");
            node.active = false;
            pfBoom.parent = node.parent;
            pfBoom.setPosition(node.position);
            let boom = pfBoom.getComponent(sp.Skeleton);
            boom.setAnimation(1, "mingzhongyanwu", false);
            this.scheduleOnce(() => {
                //销毁飞机节点
                node.active = false;
                // node.removeFromParent();
                // node.destroy();
                pfBoom.destroy();
            }, 0.4)
        }
        gameMgr.onLoadPrefabsByRes("enemyPlane/pfBoom", callback.bind(this));
    }

    update() {
        if (this.isGameEnd) return
        this.mob_lio();
    }

    mob_lio() {
        if (!this.__posData || !this.isMove) return
        switch (this.moveDirection) {
            //敌机追踪
            case 0:
                // if (!this.__posData) emitter.emit("resetPlayerPosi", this.setEndPosi.bind(this));
                this.node.setPosition(this.node.position.add(this.__posData));
                break;
            // case 1:
            // //左右移动
            // let metNum = Math.round(Math.random());
            // let metPost = metNum > 0 ? this.__posData.x : -this.__posData.x;
            // //@ts-ignore
            // this.node.setPosition(this.node.position.add(cc.v2(metPost, 0)));
            // break;
            default:
                break;
        }
    }

    setPlaneBlood(blood) {
        let uiItem = this.node.getComponent("enemyPlaneUI");
        console.log("uiItem --------------------------", uiItem)
        uiItem.setBloodProgress(blood)
    }



}
